package com.zy;

import utils.UrlUtil;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Auther: GongXl
 * @Date: 010 13:47
 * @Description:
 */
@WebServlet("/DownloadServlet")
public class DownloadServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //下载指定的目标路径
        String destPath = request.getParameter("destPath");
        //下载的文件名放进来
        String fileName = request.getParameter("fileName");
        if(null != fileName && fileName.length() > 0){
            fileName = UrlUtil.getURLDecoderString(fileName);
        }
        //告诉浏览器存储文件到磁盘
        String downLoad = request.getParameter("downLoad");
        response.setContentType("text/html;character=utf-8");
//        response.setHeader("Content-Disposition","attachment;filename="+fileName);

        if(null != downLoad && downLoad.equals("1")){
            response.setHeader("Content-Disposition","attachment;filename="+fileName);
        }else{
            if(null != fileName && fileName.length() > 0){
                String fileNameTemp = fileName.toLowerCase();
                if(fileNameTemp.indexOf(".") > -1){
                    String fileSuffix = fileNameTemp.substring(fileNameTemp.lastIndexOf(".") + 1);//abc.abc.txt
                    switch (fileSuffix){
                        case "txt":{
                            response.setContentType("text/plain;character=utf-8");
                            break;
                        }
                        case "md":{
                            response.setContentType("text/plain;character=utf-8");
                            break;
                        }
                        case "doc":{
                            response.setContentType("application/msword");
                            break;
                        }
                        case "docs":{
                            response.setContentType("application/msword");
                            break;
                        }
                        case "docx":{
                            response.setContentType("application/msword");
                            break;
                        }
                        case "pdf":{
                            response.setContentType("application/pdf");
                            break;
                        }
                        default:{
                            response.setHeader("Content-Disposition","attachment;filename="+fileName);
                        }
                    }
                }
            }
        }

        //默认下载的是项目内的文件路径
        String filePath = this.getServletContext().getRealPath("/exame_docs/"+fileName);
        if(null != destPath && destPath.length() > 0){
            if(destPath.equals("exame_docs") || destPath.equals("docs")){
                //项目内置路径
                filePath = this.getServletContext().getRealPath(File.separator+destPath+File.separator+fileName);
            }else{
                //这是指定的外部上传的文件路径
                filePath = File.separator+destPath+File.separator+fileName;
            }
        }
        //文件流输出
        ServletOutputStream outputStream = response.getOutputStream();
        InputStream in = new FileInputStream(filePath);
        byte[] buff  = new byte[1024];//1k读文件
        int len = 0;
        while ((len = in.read(buff)) > 0){
            outputStream.write(buff,0,len);
        }
        outputStream.flush();
        outputStream.close();
    }
}